package com.example.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.adapter.GoodsListAdapter;
import com.example.home.adapter.GoodsNavigationAdapter;
import com.example.home.api.ApiService;
import com.example.home.fragment.ClassifyContentFragment;
import com.example.home.fragment.GoodsListFragment;
import com.example.home.interfaces.ResponseCallback;
import com.example.home.viewmodel.GoodsList.BrotherCategory;
import com.example.home.viewmodel.GoodsList.CurrentCategory;
import com.example.home.viewmodel.GoodsList.GoodsListData;
import com.example.home.viewmodel.ResponseData;
import com.example.home.viewmodel.classify.CategoryList;

import java.util.ArrayList;
import java.util.List;

public class GoodsListActivity extends AppCompatActivity {
    private androidx.recyclerview.widget.RecyclerView goodslist_navigation;
    private CurrentCategory currentCategory;
    private List<BrotherCategory> brotherCategory;
    private TextView goodslist_list_name;
    private TextView goodslist_list_desc;
    private TextView goodslist_back;
    private GoodsNavigationAdapter goodsNavigationAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_list);
        long id = getIntent().getLongExtra("id",-1);
        int index = getIntent().getIntExtra("index",0);
        Log.e("TAG", "onCreate: "+id);
        goodslist_navigation = findViewById(R.id.goodslist_navigation);
        goodslist_back = findViewById(R.id.goodslist_back);
        goodslist_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), MainActivity.class);
                intent.putExtra("login_status",true);
                startActivity(intent);
            }
        });

        ApiService.GetGoodsNavication("" + id, new ResponseCallback<ResponseData<GoodsListData>>() {
            @Override
            public void onSuccess(ResponseData<GoodsListData> result) {
                currentCategory = result.getData().getCurrentCategory();
                brotherCategory = result.getData().getBrotherCategory();
                GoodsListFragment fragment = new GoodsListFragment();
                Bundle args = new Bundle();
                args.putLong("id",id);
                args.putString("name", currentCategory.getName());
                args.putString("desc",currentCategory.getDesc());
                fragment.setArguments(args);

                getSupportFragmentManager().beginTransaction()
                        .add(R.id.goodslist_list, fragment)
                        .commit();
                Log.d("TAG", "Data size: " + brotherCategory.size());
                goodsNavigationAdapter = new GoodsNavigationAdapter(GoodsListActivity.this, new ArrayList<>());
                goodslist_navigation.setAdapter(new GoodsNavigationAdapter(GoodsListActivity.this,brotherCategory));
                goodslist_navigation.setLayoutManager(new LinearLayoutManager(
                        GoodsListActivity.this,
                        LinearLayoutManager.HORIZONTAL,
                        false
                ));



            }
        });

    }

    public void selectItem(int index){
        if (brotherCategory == null || index < 0 || index >= brotherCategory.size()) {
            return;
        }

//        for (CategoryList item : categoryList) {
//            item.setChecked(false);
//        }
//        categoryList.get(index).setChecked(true);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        GoodsListFragment ccf = GoodsListFragment.newInstance(
                brotherCategory.get(index).getId()+"", ""
        );
        ft.replace(R.id.goodslist_list, ccf);
        ft.commitAllowingStateLoss();


        goodsNavigationAdapter.notifyDataSetChanged();

    }

}